//index.js
import Toast from 'vant-weapp/toast/toast';
//获取应用实例
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
Page({
  data: {
    moduleArray: [{
        name: '我要发货',
        icon: '../../images/icon_goods.png',
        link: '/pages/deliver-goods/deliver-goods'
      },
      {
        name: '订单确认',
        icon: '../../images/icon_order.png',
        link: '/pages/confirm-order/confirm-order'
      }, {
        name: '全部食材',
        icon: '../../images/icon_food.png',
        link: '/pages/all-ingredients/all-ingredients'
      },
      {
        name: '个人中心',
        icon: '../../images/icon_person.png',
        link: '/pages/personal-center/personal-center'
      }
    ],
    loading: false,
    color: '#fff',
    background: '#24b569',
    show: true,
    sureNum: 0,
    sendNum: 0,
    title: "",
    animated: false,
    orderList: []
  },
  onLoad: function(options) {
    const toast = Toast.loading({
      duration: 0, // 持续展示 toast
      forbidClick: true, // 禁用背景点击
      message: '加载中...',
      loadingType: 'spinner',
      selector: '#van-toast'
    });
    this.initModule();
  },
  onShow: function() {
    app.loginCompleteCallBack(callback => {
      console.log("index.wxml");
      this.initDatas();
    }, {})
    // setTimeout(function() {
    //   Toast.clear();
    // }, 10000);
  },
  initDatas() {
    let personInfo = userInfoManager.getSupplierInfo();
    if (!personInfo) return;
    http.getOrderDettails(personInfo.id, 2).then(res => {
      if (res.code == 0) {
        for (var i = 0; i < res.data.length; i++) {
          res.data[i].endTime = res.data[i].arrivalDateEnd.split(" ")[1];
        }
        this.setData({
            orderList: res.data
          },
          function() {
            Toast.clear();
          });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
    http.getNum(personInfo.id).then(res => {
      if (res.code == 0) {
        if (res.data.sureNum > 99) {
          res.data.sureNum = 99;
        }
        if (res.data.sendNum > 99) {
          res.data.sendNum = 99;
        }
        this.setData({
          sureNum: res.data.sureNum,
          sendNum: res.data.sendNum
        });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })

  },
  /**点击详情 */
  clickGardenDetail(event) {
    console.log(event);
    wx.navigateTo({
      url: '/pages/deliver-goods/deliver-goods-detail?orderId=' + event.currentTarget.dataset.item.orderId,
      success: (result) => {

      },
      fail: (e) => {
        console.log(JSON.stringify(e))
      },
      complete: () => {}
    });
  },
  /**
   * 初始化模块信息
   */
  initModule: function() {
    let thos = this;
    this.setData({
      moduleArray: thos.data.moduleArray
    });
    let loginAccountInfo = userInfoManager.getUserInfo();
    if (!loginAccountInfo) {
      http.getCurrentUserInfo().then(res => {
        if (res.code == 0) {
          if (res.data) {
            userInfoManager.setUserInfo(res.data);
            return http.supplierInfo(res.data.userUnionId);
          } else {
            wx.showToast({
              duration: 1000,
              icon: 'none',
              title: "当前账号异常，请联系管理员"
            })
            return Promise.reject();
          }
        } else {
          return Promise.reject();
        }
      }).then(res => {
        if (res.code == 0) {
          if (res.data) {
            userInfoManager.setSupplierInfo(res.data);
            this.setData({
              title: res.data.name
            });
            this.initDatas();
          } else {
            wx.showToast({
              duration: 1000,
              icon: 'none',
              title: "当前账号异常，请联系管理员"
            })
          }

        } else {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: res.msg
          })
        }
      })
    } else {
      http.supplierInfo(loginAccountInfo.userUnionId).then(res => {
        if (res.code == 0) {
          if (res.data) {
            userInfoManager.setSupplierInfo(res.data);
            this.setData({
              title: res.data.name
            });
            this.initDatas();
          } else {
            wx.showToast({
              duration: 1000,
              icon: 'none',
              title: "当前账号异常，请联系管理员"
            })
          }
        } else {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: res.msg
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    Toast.clear();
  },
})